package com.study.concurrent.A_ThreadManagement.g_CreateDaemonThreadAndRun;

import java.util.Date;
import java.util.Deque;
import java.util.concurrent.TimeUnit;

/**
 * Created by 赵超 on 2017/3/18.
 */
public class WriterTask implements Runnable {
    private Deque<Event> deque;

    public WriterTask(Deque<Event> deque) {
        this.deque = deque;
    }

    /**
     * 执行100次循环，每次循环中都会创建一个新的Event对象并放到队列中，然后休眠一秒钟
     */
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            Event event = new Event();
            event.setDate(new Date());
            event.setEvent(String.format("线程 %s 创建了一个event对象",
                    Thread.currentThread().getId()));
            deque.addFirst(event);
            //System.out.println(event.getEvent() + " - " + event.getDate());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
